package com.bbm.bbmanage.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bbm.bbmanage.entity.AppHttpCodeEnum;
import com.bbm.bbmanage.entity.Comment;
import com.bbm.bbmanage.entity.House;
import com.bbm.bbmanage.entity.User;
import com.bbm.bbmanage.entity.vo.CommentVo;
import com.bbm.bbmanage.entity.vo.FinishedCommentVo;
import com.bbm.bbmanage.entity.vo.PageVo;
import com.bbm.bbmanage.entity.vo.ResponseResult;
import com.bbm.bbmanage.mapper.CommentMapper;
import com.bbm.bbmanage.service.CommentService;
import com.bbm.bbmanage.service.HouseService;
import com.bbm.bbmanage.service.UserService;
import com.bbm.bbmanage.util.BeanCopyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * (Comment)表服务实现类
 *
 * @author lionkliu
 * @since 2023-12-07 14:30:00
 */
@Service("commentService")
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements CommentService {

    @Autowired
    private UserService userService;

    @Resource
    private HouseService houseService;

    @Override
    public ResponseResult selectPage(Integer pageNum, Integer pageSize) {
        LambdaQueryWrapper<Comment> queryWrapper = new LambdaQueryWrapper<>();
        Page<Comment> page = new Page<>(pageNum, pageSize);
        Page<Comment> userPage = page(page, queryWrapper);
        PageVo<Comment> pageVo = new PageVo<>();
        pageVo.setResult(userPage.getRecords());
        pageVo.setTotal(userPage.getTotal());
        pageVo.setPageTotal(pageVo.getPageTotal(pageSize));
        return ResponseResult.okResult(pageVo);
    }

    @Override
    public ResponseResult selectOne(Integer id) {
        return ResponseResult.okResult(getById(id));
    }

    @Override
    public ResponseResult insertOne(Comment comment) {
        return save(comment) ? ResponseResult.okResult() : ResponseResult.errorResult(AppHttpCodeEnum.INSERT_ERROR);
    }

    @Override
    public ResponseResult deleteById(Integer id) {
        return removeById(id) ? ResponseResult.okResult() : ResponseResult.errorResult(AppHttpCodeEnum.DELETE_ERROR);
    }

    @Override
    public ResponseResult updateOne(Comment comment) {
        UpdateWrapper<Comment> updateWrapper = new UpdateWrapper();
        // TODO：
        return update(updateWrapper) ? ResponseResult.okResult() : ResponseResult.errorResult(AppHttpCodeEnum.UPDATE_ERROR);
    }

    @Override
    public ResponseResult getCommentByHouseId(Long id) {
        LambdaQueryWrapper<Comment> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByDesc(Comment::getCreateTime);
        queryWrapper.eq(Comment::getHouseId, id);
        List<Comment> list = list(queryWrapper);
        List<CommentVo> commentVos = BeanCopyUtils.copyBeanList(list, CommentVo.class);
        List<CommentVo> collect = commentVos.stream().map(commentVo -> {
            LambdaQueryWrapper<User> userWrapper = new LambdaQueryWrapper<>();
            userWrapper.eq(User::getId, commentVo.getUserId());
            User user = userService.getOne(userWrapper);
            commentVo.setAvatar(user.getAvatar());
            commentVo.setUsername(user.getUsername());
            commentVo.setNickName(user.getNickName());
            return commentVo;
        }).collect(Collectors.toList());

        return ResponseResult.okResult(collect);
    }

    @Override
    public ResponseResult search(Integer pageNum, Integer pageSize, Integer userId, Integer houseId, String content) {
        LambdaQueryWrapper<Comment> queryWrapper = new LambdaQueryWrapper<>();
        if (!Objects.isNull(userId)) {
            queryWrapper.eq(Comment::getUserId, userId);
        }
        if (!Objects.isNull(houseId)) {
            queryWrapper.eq(Comment::getHouseId, houseId);
        }
        if (!Objects.isNull(content)) {
            queryWrapper.like(Comment::getContent, content);
        }
        Page<Comment> userPage = new Page<>(pageNum, pageSize);
        page(userPage, queryWrapper);
        PageVo<Comment> userPageVo = new PageVo<>();
        userPageVo.setTotal(userPage.getTotal());
        userPageVo.setPageTotal(userPage.getPages());
        userPageVo.setResult(userPage.getRecords());

        return ResponseResult.okResult(userPageVo);
    }

    @Override
    public ResponseResult getFinishedComment(Long userId) {
        LambdaQueryWrapper<Comment> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Comment::getUserId, userId);
        List<Comment> commentList = list(queryWrapper);

        List<FinishedCommentVo> collect = commentList.stream().map(comment -> {
            FinishedCommentVo commentVo = BeanCopyUtils.copyBean(comment, FinishedCommentVo.class);
            House house = houseService.getById(comment.getHouseId());
            if (house != null) {
                commentVo.setTitle(house.getTitle())
                        .setImg(house.getImg())
                        .setDes(house.getDes())
                        .setPrice(house.getPrice());
            }

            return commentVo;
        }).collect(Collectors.toList());
        return ResponseResult.okResult(collect);
    }

}

